import { notification } from 'antd';
import {
  addClient, delClient,
  getClientById,
  getClientPage, updClient,
} from "../services/client";

export default {
  namespace: 'client',

  state: {
    data: {
      list: [],
      pagination: {},
    },
    selectedClient: {},
    modal: {
      width: (window.innerWidth / 2),
      visible: false,
      title: '',
      mode: 'None',
    },
    searchParams: {},
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(getClientPage, payload);
      if (response) {
        const { records, total, size, current } = response;
        const data = {
          list: records,
          pagination: {
            total,
            pageSize: size,
            current,
          },
        };
        yield put({
          type: 'save',
          payload: {
            data,
          },
        });
      }
    },
    *operation({ payload }, { call, put }) {
      const { mode, clientId } = payload;
      if (mode === 'Add') {
        yield put({
          type: 'changeModalStatus',
          payload: {
            selectedClient: {},
            modal: {
              visible: true,
              title: '新增客户端',
              mode,
            },
          },
        });
      } else if (mode === 'Update') {
        const response = yield call(getClientById, { clientId });
        if (response) {
          yield put({
            type: 'changeModalStatus',
            payload: {
              selectedClient: response,
              modal: {
                visible: true,
                title: '修改客户端',
                mode,
              },
            },
          });
        } else {
          yield put({
            type: 'initModal',
          });
          notification.error({
            message: '数据错误',
            description: '你选择的数据可能已经不存在，请刷新列表再试！',
          });
        }
      } else if (mode === 'Delete') {
        const response = yield call(getClientById, { clientId });
        if (response) {
          yield put({
            type: 'del',
            payload: {
              clientId,
            },
          });
        } else {
          yield put({
            type: 'initModal',
          });
          notification.error({
            message: '数据错误',
            description: '你选择的数据可能已经不存在，请刷新列表再试！',
          });
        }
      } else {
        yield put({
          type: 'initModal',
        });
      }
    },
    *add({ payload }, { call, put, select }) {
      const response = yield call(addClient, payload);
      if (response && response.msg === 'success') {
        const searchParams = yield select(state => state.client.searchParams);
        yield put({
          type: 'initModal',
        });
        yield put({
          type: 'fetch',
          payload: searchParams,
        });
      } else {
        notification.error({
          message: '新增客户端失败',
        });
      }
    },
    *del({ payload }, { call, put, select }) {
      const response = yield call(delClient, payload);
      if (response && response.msg === 'success') {
        const searchParams = yield select(state => state.client.searchParams);
        yield put({
          type: 'fetch',
          payload: searchParams,
        });
      } else {
        notification.error({
          message: '删除数据失败',
          description: '你选择的数据可能已经不存在，请刷新列表再试！',
        });
      }
    },
    *upd({ payload }, { call, put, select }) {
      const response = yield call(updClient, payload);
      if (response && response.msg === 'success') {
        const searchParams = yield select(state => state.client.searchParams);
        yield put({
          type: 'initModal',
        });
        yield put({
          type: 'fetch',
          payload: searchParams,
        });
      } else {
        notification.error({
          message: '修改路由失败',
          description: '你选择的数据可能已经不存在，请刷新列表再试！',
        });
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    saveSearchParams(state, { payload }) {
      return {
        ...state,
        searchParams: {
          ...payload,
        },
      };
    },
    changeSelectedClient(state, { payload }) {
      return {
        ...state,
        selectedClient: {
          ...state.selectedClient,
          ...payload,
        },
      };
    },
    clearSelectedClient(state) {
      return {
        ...state,
        selectedClient: {},
      };
    },
    initModal(state) {
      return {
        ...state,
        selectedClient: {},
        modal: {
          width: (window.innerWidth / 2),
          visible: false,
          title: '',
          mode: 'None',
        },
      };
    },
    changeModalStatus(state, { payload }) {
      return {
        ...state,
        selectedClient: {
          ...payload.selectedClient,
        },
        modal: {
          ...state.modal,
          ...payload.modal,
        },
      };
    },
  },
}
